<?php
/**
 * Description
 *
 * PHP Version 5.6
 *
 * @category TelegramPostBot
 * @package  Data
 * @author   ctwj <908504609@qq.com>
 * @license  MIT https://mit-license.org/
 * @link     https://mit-license.org/
 * @date     2018-12-21 10:48:21
 * @modifyby ctwj
 */

namespace Telegram\Data;

/**
 * Message class
 *
 * 消息
 *
 * @category PHP
 * @package  Data
 * @author   ctwj <908504609@qq.com>
 * @license  MIT https://mit-license.org/
 * @link     MIT https://mit-license.org/
 */
class UserModel extends BaseModel
{

    /**
     * 构造函数
     *
     * @param [type] $c Container
     *
     * @return void
     */
    public function __construct($c)
    {
        $this->table = $this->tableName('user');
        parent::__construct($c);
    }

    /**
     * 验证密码是否正确
     *
     * @param [string] $username 用户名
     * @param [string] $password 密码
     *
     * @return boolean
     */
    public function validPassword($username, $password)
    {
        $user = $this->get(['username' => $username]);
        if (!$user) {
            return false;
        }
        $str = $user['slat'] . $password . $user['slat'];
        if (md5($str) == $user['password']) {
            return true;
        }
        return false;
    }

    /**
     * 更新密码
     *
     * @param [string] $username 用户名
     * @param [string] $oldpass  旧密码
     * @param [string] $newpass  新密码
     *
     * @return boolean
     */
    public function updatePassword($username, $oldpass, $newpass)
    {
        if (!$this->validPassword($username, $password)) {
            return false;
        }

        $str = $user['slat'] . $password . $user['slat'];
        $pass = md5($str);
        if ($this->update(['password' => $pass], ['username' => $username])) {
            return true;
        }
        return false;
    }
}
